package de.nordakademie.wpk.share.core.data;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.GregorianCalendar;
import java.util.LinkedHashMap;

import de.nordakademie.wpk.share.core.data.currency.Currency;
import de.nordakademie.wpk.share.core.data.treeitem.TreeItem;

/**
 * This class represents a share on the server
 * 
 * @author Christian Baumhof
 */
public class Share extends TreeItem implements Serializable {

	private static final long serialVersionUID = -776889364569548417L;

	private String symbol;
	private String name;
	private String cusipNumber;

	private Currency currency;

	private Integer quantity = 1;

	private String annRange = "";
	private BigDecimal quotation = new BigDecimal(0);
	private BigDecimal previousClose = new BigDecimal(0);
	private String percentageChange = "0%";
	private LinkedHashMap<GregorianCalendar, BigDecimal> historicQuotes = new LinkedHashMap<GregorianCalendar, BigDecimal>();

	public Share() {
	}

	public Share(String symbol, String name, String cusipNumber,
			Currency currency) {
		this.symbol = symbol;
		this.name = name;
		this.cusipNumber = cusipNumber;
		this.currency = currency;
	}

	public Share(String symbol, String name, String cusipNumber,
			String annRange, Currency currency, BigDecimal quotation,
			String percentageChange) {
		this.symbol = symbol;
		this.name = name;
		this.cusipNumber = cusipNumber;
		this.currency = currency;
		this.annRange = annRange;
		this.quotation = quotation;
		this.percentageChange = percentageChange;
	}

	public String getSymbol() {
		return symbol;
	}

	public String getName() {
		return name;
	}

	public BigDecimal getTotalValue() {
		return quotation.multiply(new BigDecimal(quantity));
	}

	/**
	 * Returns the average quote for the last 30 days
	 * 
	 * @return BigDecimal
	 */
	public BigDecimal getAverageQuote() {
		BigDecimal avgQuote = new BigDecimal(0);
		int i = 0;
		for (BigDecimal quote : historicQuotes.values()) {
			avgQuote = avgQuote.add(quote);
			i++;
		}
		if (i != 0) {
			avgQuote = avgQuote.divide(new BigDecimal(i),2);
		}
		return avgQuote;
	}

	public String getCusipNumber() {
		return cusipNumber;
	}

	public Integer getQuantity() {
		return quantity;
	}

	public void setQuantity(Integer quantity) {
		this.quantity = quantity;
	}

	public BigDecimal getRoundQuote() {
		return quotation;
	}

	public Currency getCurrency() {
		return currency;
	}

	public void setAnnRange(String annRange) {
		this.annRange = annRange;
	}

	public String getAnnRange() {
		return annRange;
	}

	public void setPercentageChange(String percentageChange) {
		this.percentageChange = percentageChange;
	}

	public String getPercentageChange() {
		return percentageChange;
	}

	public void setQuotation(BigDecimal quotation) {
		this.quotation = quotation;
	}

	public BigDecimal getQuotation() {
		return quotation;
	}

	/**
	 * Returns a HashMap containing the quotes from the last 30 days
	 * 
	 * @return HashMap
	 */
	public LinkedHashMap<GregorianCalendar, BigDecimal> getHistoricQuotes() {
		return historicQuotes;
	}

	public void setHistoricQuotes(
			LinkedHashMap<GregorianCalendar, BigDecimal> historicQuotes) {
		this.historicQuotes = historicQuotes;
	}

	@Override
	public Type getType() {
		return TreeItem.Type.SHARE;
	}

	public BigDecimal getPreviousClose() {
		return previousClose;
	}

	public void setPreviousClose(BigDecimal previousClose) {
		this.previousClose = previousClose;
	}

}
